package com.jxbeidou.generate.controller;

import com.jxbeidou.core.constents.ValidateConstants;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.core.validate.ModifyValidateGroup;
import com.jxbeidou.core.validate.SaveValidateGroup;
import com.jxbeidou.generate.dto.TableAndGenerateDTO;
import com.jxbeidou.generate.dto.TableSaveDTO;
import com.jxbeidou.generate.query.page.TablePageQueryDTO;
import com.jxbeidou.generate.service.TableService;
import com.jxbeidou.generate.vo.FieldVO;
import com.jxbeidou.generate.vo.ResultKeyVO;
import com.jxbeidou.generate.vo.TableAndGenerateVO;
import com.jxbeidou.generate.vo.TableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: jxbeidou-basic
 * @ClassName: TableController
 * @description: 数据表管理
 * @author: 杜晓祥
 * @create: 2024-03-13 11:36
 * 版权所有：交信北斗(北京)信息科技有些公司
 */
@Slf4j
@RestController
@RequestMapping("/generate/table")
@Tag(name = "数据表管理")
public class TableController {
    
    @Resource
    private TableService tableService;


    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public R<PageResult<TableVO>> page(@RequestBody @Valid TablePageQueryDTO query){
        PageResult<TableVO> page = tableService.page(query);
        return R.success(page);
    }


    @PostMapping("/listByTable/{id}")
    @Operation(summary = "查询指定表的所有字段信息，并生成唯一 KEY")
    public R<ResultKeyVO<TableAndGenerateVO, List<FieldVO>>> listForKey (@PathVariable("id") @NotNull(message = "表 ID 不能为空") Long id) {
        ResultKeyVO<TableAndGenerateVO, List<FieldVO>> result = tableService.listForKey(id);
        return R.success(result);
    }

    @PutMapping("/modify/generate")
    public R<ResultKeyVO<TableAndGenerateVO, List<FieldVO>>> updateByGenerate (@RequestBody @Validated(ModifyValidateGroup.class) TableAndGenerateDTO tableAndGenerate) {
        ResultKeyVO<TableAndGenerateVO, List<FieldVO>> result = tableService.updateBaseInfo(tableAndGenerate);
        return R.success(result);
    }

    @PostMapping("generate/{id}")
    public R<TableAndGenerateVO> generateForTable (@PathVariable("id") @NotNull(message = "表 ID 不能为空") Long id) {
         return R.success(tableService.generateForTable(id));
    }

    @GetMapping("/{id}")
    @Operation(summary = "ID查询")
    public R<TableVO> get(@PathVariable("id") @NotNull(message = ValidateConstants.FIND_ID_NULL) Long id){
        return R.success(tableService.findById(id));
    }

    @PostMapping
    @Operation(summary = "新增")
    public R<String> save(@RequestBody @Validated(SaveValidateGroup.class) TableSaveDTO vo){
        tableService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改")
    public R<String> update(@RequestBody @Validated(ModifyValidateGroup.class) TableSaveDTO vo){
        tableService.modify(vo);
        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "ID删除")
    public R<String> delete(@PathVariable("id") @NotNull(message = ValidateConstants.DELETE_ID_NULL) Long id){
        tableService.delete(id);
        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody @NotEmpty(message = ValidateConstants.DELETE_IDS_NULL) List<Long> idList) {
        tableService.deleteForList(idList);
        return R.deleteSuccess();
    }
    
}
